//
//  HMWelcomeViewController.swift
//  Weibo
//
//  Created by teacher on 2017/6/7.
//  Copyright © 2017年 teacher. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

//欢迎页面
class HMWelcomeViewController: UIViewController {

    //  MARK:   --懒加载控件
    fileprivate lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //  用户头像
    fileprivate lazy var userImageView: UIImageView =  {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        //  设置圆形，裁剪到多余部分
        imageView.layer.cornerRadius = 45
        imageView.layer.masksToBounds = true
        //  判断头像地址不为nil显示头像
        if let imageUrl = HMUserAccountViewModel.sharedViewModel.userAccount?.avatar_large {
            imageView.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        return imageView
    }()
    
    //  欢迎信息
    fileprivate lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 14)
        label.textColor = UIColor.darkGray
        //  判断用户名不为nil，显示用户名
        if let name = HMUserAccountViewModel.sharedViewModel.userAccount?.name {
            label.text = "欢迎回来，\(name)"
        } else {
            label.text = "欢迎回来"
        }
        //  设置默认透明度
        label.alpha = 0
        return label
    }()
    
    //  自定义视图控制器的view
    override func loadView() {
        view = bgImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        
    }
    
    //  需要等到视图已经显示执行动画
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        startSpringAnimation()
    }

    //  添加控件设置约束
    private func setupUI() {
        view.addSubview(userImageView)
        view.addSubview(messageLabel)
        
        userImageView.snp.makeConstraints { (make) in
            make.top.equalTo(self.view).offset(200)
            make.centerX.equalTo(self.view)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        messageLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userImageView.snp.bottom).offset(10)
            make.centerX.equalTo(userImageView)
        }
    }
    
    //  开启弹簧动画
    private func startSpringAnimation() {
        //  更新头像的顶部约束
        userImageView.snp.updateConstraints { (make) in
            make.top.equalTo(self.view).offset(100)
        }
        //  开启弹簧动画
        /*
            1.Duration: 动画时长
            2.Damping : 阻尼系数 0-1
            3.Velocity: 初始速度
            4.options : 选项
            5.动画执行的代码块
            6.动画完成执行的代码块
         */
        
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 10, options: [], animations: {
            //  约束动画
            self.view.layoutIfNeeded()
        }) { (_) in
            UIView.animate(withDuration: 1, animations: { 
                self.messageLabel.alpha = 1
            }, completion: { (_) in
                //  切换到首页
                NotificationCenter.default.post(name: NSNotification.Name(SwitchRootVCNotificationName), object: nil)
            })
        }
        
    
    }
  

}
